package com.letoken.platform.repo.utlis;


import com.letoken.platform.pub.enums.ApiResponseEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.admin.redis.AdminUserInfoRedis;
import com.letoken.platform.repo.admin.res.user.AdminUserInfoRes;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.app.redis.AppLoginTokenRedis;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/26 14:12
 */
public class WebUtils {

    public WebUtils() {
    }

    public static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    public static String getToken() {
        return getHttpServletRequest().getHeader("token");
    }


    public static Integer getUserId() {
        String token = getHttpServletRequest().getHeader("token");
        Integer userId = null;
        if (!StringUtils.isBlank(token)) {
            AppUser appUser = AppLoginTokenRedis.me().getClassObject(token);
            if (!ObjectUtils.isEmpty(appUser)) {
                userId = appUser.getId();
            }
        }
        return userId;
    }

    /*public static AppUser getUser() {
        String token = getHttpServletRequest().getHeader("token");
        if (!StringUtils.isBlank(token)) {
            AppUser appUser = AppLoginTokenRedis.me().getClassObject(token);
            if (!ObjectUtils.isEmpty(appUser)) {
                return appUser;
            }
        }
        throw  new RuntimeException("not found user info!!!!!");
    }*/


    public static String getUserToken() {
        String token = getHttpServletRequest().getHeader("token");
        if (StringUtils.isBlank(token)) {
            throw new ApiServiceException(ApiResponseEnum.ERROR_004, new Object[0]);
        } else {
            return token;
        }
    }

    public static String getUserAddress() {
        String token = getHttpServletRequest().getHeader("address");
        if (StringUtils.isBlank(token)) {
            throw new ApiServiceException(ApiResponseEnum.ERROR_004, new Object[0]);
        } else {
            return token;
        }
    }


    /**
     * 获取 getAuthorization
     *
     * @return
     */
    public static String getAuthorization() {
        String authorization = getHttpServletRequest().getHeader("authorization");
        if (StringUtils.isBlank(authorization)) {
            throw new ApiServiceException(ApiResponseEnum.ERROR_004);
        }
        return authorization;
    }

    public static AppUser getUser() {
        String token = getHttpServletRequest().getHeader("token");
        if (!StringUtils.isBlank(token)) {
            AppUser appUser = AppLoginTokenRedis.me().getClassObject(token);
            if (!ObjectUtils.isEmpty(appUser)) {
                return appUser;
            }
        }
        throw  new RuntimeException("not found user info!!!!!");
    }

    /**
     * 获取 language
     *
     * @return
     */
    public static String getLanguage() {
        String language = getHttpServletRequest().getHeader("language");
        if (StringUtils.isBlank(language)) {
            language = "zh_CN";
        }
        return language;
    }

    
    /**
     * 获取 AdminUserId
     *
     * @return
     */
    public static Integer getAdminUserId() {
        String authorization = getHttpServletRequest().getHeader("authorization");
        if (StringUtils.isBlank(authorization)) {
            return null;
        }
        AdminUserInfoRes res = AdminUserInfoRedis.me().getAdminUserInfo(authorization);
        if (res == null){
            return null;
        }
        return res.getAdminUserId();
    }
}
